<?php

namespace app\admin\controller;

use app\common\controller\BaseAdminController;
use app\common\library\addon\Addon;
use app\common\model\Addons;

class Addonhand extends BaseAdminController
{
    public function index()
    {
        if($this->request->isAjax()){
            $res = $this->getList();
            return json($res);
        }
        return $this->view->fetch();
    }
    protected function getList()
    {
        [$where, $sort, $order, $offset, $limit] = $this->buildparams(whereIsArray: true);
        $addonsList = get_addons_list();
        foreach ($addonsList as $key => $value){
            $addonsList[$key] = get_addons_info($value);
        }
        $appList = get_app_list();
        foreach ($appList as $key => $value){
            $appList[$key] = get_app_info($value);
        }
        $length = count($addonsList)+count($appList);
        $bd_list = array_merge($addonsList,$appList);
        $yc_length = 0;
        if($length>=($offset+$limit)){
            $list = array_slice($bd_list,$offset,$limit);
        }else{
            $list = $bd_list;
            $yc_length = $offset+$limit-$length;
            $yc_array = $this->getYcList($length,$yc_length);

            $list = array_merge($list,$yc_array);
        }
        return [
            'rows' => $list,
            'total' => $length+$yc_length
        ];
    }
    protected function getYcList($start,$length)
    {
        return [];
    }

    /**
     * 启用/禁用
     * @return mixed
     * @throws \Exception
     */
    public function change()
    {
        $data = $this->request->param();
        if(empty($data['myaddon'])){
            return $this->error('参数错误');
        }
        $addon = $data['myaddon'];
        $state = $data['state'];
        if($state==0){
            try {
                Addon::disable($addon);
            }catch (\Exception $e){
                return $this->error($e->getMessage());
            }

        }elseif ($state==1){
            try {
                Addon::enable($addon);
            }catch (\Exception $e){
                return $this->error($e->getMessage());
            }

        }
        return $this->success('操作成功');
    }

    /**
     * 添加插件
     * @return mixed
     * @throws \Exception
     */
    public function add_addon()
    {
        if($this->request->isAjax()){
            $data = $this->request->param('row/a');
            try {
                Addon::createAddons(
                    addon:$data['name'],
                    type: $data['type'],
                    title: $data['title'],
                    intro: $data['intro'],
                    author:$data['author'],
                    website:$data['website']??"",
                    autorouter:$data['autorouter']??"",
                    version: $data['version']??"",
                    require: $data['require']??""
                );
            }catch (\Exception $e){
                return $this->error($e->getMessage());
            }
            return $this->success('操作成功');
        }
        return $this->view->fetch();
    }

    /**
     * 卸载插件
     * @return mixed
     * @throws \Exception
     */
    public function uninstall()
    {
        $addon = $this->request->param('myaddon');
        if(empty($addon)){
            return $this->error('参数错误');
        }
//        Addon::uninstall($addon);
        try {
            Addon::uninstall($addon);
        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
        return $this->success('操作成功');
    }

    /**
     * 获取插件列表
     * @return \think\response\Json
     */
    public function get_modules()
    {
        $addons = get_addons_list();
        foreach ($addons as $key => $value){
            $tem = get_addons_info($value);
            $tem['title'] = $tem['title'].'--[addons]';
            $addons[$key] = $tem;
        }
        $apps = get_app_list();
        foreach ($apps as $key => $value){
            $tem = get_app_info($value);
            $tem['title'] = $tem['title'].'--[app]';
            $apps[$key] = $tem;
        }
        $allList = array_merge($addons,$apps);
        return json(['tital'=>count($allList),'rows'=>$allList]);
    }

    /**
     * 插件配置
     * @return mixed
     * @throws \Exception
     */
    public function config()
    {
        $addon = $this->request->param('myaddon');
        if(empty($addon)){
            return $this->error('参数错误');
        }
        $addonObj = Addons::where('name',$addon)->find();
        if(empty($addonObj)){
            return $this->error('插件不存在');
        }
        if($this->request->isAjax()){
            $data = $this->request->param('row/a');
            try {
                if($addonObj->type=='addons'){
                    set_addons_config($addon,$data);
                }else{
                    set_app_config($addon,$data);
                }

            }catch (\Exception $e){
                return $this->error($e->getMessage());
            }
            return $this->success('操作成功');
        }
        if($addonObj->type=='addons'){
            $info = get_addons_config($addon);
            $row = get_addons_config($addon,true);
        }else{
            $info = get_app_config($addon);
            $row = get_app_config($addon,true);
        }
        $this->assignconfig('info',$info);
//        echo '<pre>';
//        print_r($row);
//        exit;
        $this->assignconfig('row',$row);
        $this->assignconfig('myaddon',$addon);
        return $this->view->fetch();
    }

    /**
     * 打包插件
     * @return mixed
     * @throws \Exception
     */
    public function pack()
    {
        $addon = $this->request->param('myaddon');
        if(empty($addon)){
            return $this->error('参数错误');
        }
        try {
            $filePath = Addon::pack($addon);
        }catch (\Exception $e){
            return $this->error($e->getMessage(),'',$e->getTrace());
        }
        return $this->success('打包成功,zip包地址为:'.$filePath);
    }

    /**
     * 本地安装插件
     * @return mixed
     * @throws \Exception
     */
    public function install()
    {
        $fileUrl = $this->request->param('fileUrl');
        if(empty($fileUrl)){
            return $this->error('参数错误');
        }
        $fileUrl = root_path().'public'.$fileUrl;
        try{
            Addon::install($fileUrl);
        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
        return $this->success('安装成功');
    }
}